#include "canbusoff.h"
#include "hal_can.h"
#include "hal_stm.h"
uint8_t Fast_Count = 0;
busCanTxMsg BusTxMessage = {0};                /* ���ͻ����� */

static uint32_t NowtimeCount = 0;
uint32_t TT_GetNowCount(void)
{
  return NowtimeCount;
}

uint32_t TT_GetDelayT_MS(uint32_t Last_time_count)
{
  uint32_t count = 0;
  count = TT_GetNowCount();
  return count - Last_time_count;
}

void TT_GetDelayT_MS_CallBak(void)
{
  NowtimeCount++;
  hal_stm_resetcount(STM_CHANNEL0);
  hal_stm_enabled(STM_CHANNEL0,HAL_ENABLE);
}

/**
  * @brief  ���ݷ�������
  * @param  CANx
  * @retval  None
  */
void CANbusoff_SetMsg(busCanTxMsg *BusTxMessage)
{
    uint8_t ubCounter = 0;

    BusTxMessage->StdId = 0x6AD;                 /* ʹ�ñ�׼ID */
    BusTxMessage->DLC = 8;                       /* ���ݳ���Ϊ8�ֽ� */


    /* ����Ҫ���͵�����0-7 */
    for(ubCounter = 0; ubCounter < 8; ubCounter++)
    {
        BusTxMessage->Data[ubCounter] = 10;
    }

}
uint32_t First_timing(uint32_t count)
{
  if(0 == count)
  {
    count = TT_GetNowCount();
    return count;
  }
}
void CanBusOff_Recover()
{   
  static uint32_t count = 0;
  count = First_timing(count);

    if((5 > Fast_Count) &&(TT_GetDelayT_MS(count) > DELAY_100MS))
    {
      if (CONTROLLERMODE_BUSOFF == hal_can_get_ctrl_mode(CAN_CHANNEL0))
      {
        hal_can_init_test();
        Fast_Count+=1;
      }else
      {
        Fast_Count = 0;
      }
      count = TT_GetNowCount();
    }else if( (5 <= Fast_Count) && ( DELAY_1000MS < TT_GetDelayT_MS(count)))
    {
      if (CONTROLLERMODE_BUSOFF == hal_can_get_ctrl_mode(CAN_CHANNEL0))
      {
        hal_can_init_test();
      }else
      {
        Fast_Count = 0;
      }
      count = TT_GetNowCount();
    }else
    {
      
    }
}

